<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Model\CardHolder;
use App\Model\Cards;
use App\Model\UserActivity;
use App\Model\UserTemplate;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class ProfileController extends Controller
{

    /**
     * @var User
     */
    private $user = null;

    public function __construct()
    {
        $this->user = Auth::user();
    }

    public function index(Cards $cards)
    {
        $card = $cards->where('user_id', '=', $this->user->id)->first();
        $user = $this->user;
        $is_perfect = 1;
        $attr = ['user_name','mobile_phone','head_picture', 'sex', 'birthday','industry', 'profession', 'company', 'motto','province','city','area', 'address_detail', 'wechat_code', 'email', 'website'];
        foreach ($attr as $at) {
            if ($at == 'mobile_phone') {
                if (empty($card->$at)) {
                    if (empty($user->phone)){
                        $is_perfect -= 0.22;
                    }
                }
                continue;
            }
            if (in_array($at, ['province','city','area'])) {
                if (empty($card->$at)) {
                    $is_perfect -= 0.02;
                }
                continue;
            }
            if (in_array($at, ['user_name', 'head_picture'])) {
                if (empty($card->$at) && empty($user->detail)) {
                    $is_perfect -= 0.02;
                    continue;
                }
                if ($at == 'user_name' && empty($user->detail->wechat_name)) {
                    $is_perfect -= 0.02;
                }
                if ($at == 'head_picture' && empty($user->detail->head_portrait)) {
                    $is_perfect -= 0.02;
                }
                continue;
            }
            if (blank(optional($card)->$at)){
                $is_perfect -= 0.06;
            }
        }

        $activity = UserActivity::where("user_id", $user->id)->first();

        $wechat_name = isset($user->detail->wechat_name) ?$user->detail->wechat_name :"";
        $is_per = isset($user->cards) ? true :false;
        $data = [
            'id' => $user->id,
            'name' => $user->cards ? $user->cards->user_name : $wechat_name,
            'portrait' => $user->detail ? $user->detail->head_portrait : '',
            'phone' => $user->phone,
            'industry' => $user->cards ? $user->cards->industry : '',
            'profession' => $user->cards ? $user->cards->profession : '',
            'company' => $user->cards ? $user->cards->company : '',
            'qq' => $user->cards ? $user->cards->qq : '',
            'email' => $user->email,
            'vip_level' => User::is_vip($user->id)?:0,
            'card_id' => $card ? $card->id : 0,
            'card_num' => $user->cardHolders ? $user->cardHolders()->count() : 0,
            'is_perfect' => round($is_perfect, 2),
            'motto' => $card?$card->motto:'',
            'payment_pwd' => $user->payment_password?true:false,
            'saved' => $user->cards?CardHolder::where('card_id', $user->cards->id)->count():0,
            'type' => $user->type,
            'money' => $user->money,
            'maibao' => intval($user->maibao),
            'reward' => $user->currentCommission,
            'is_activity' => $activity ? true : false,
            'invitation_code' => isset($user->invitation_code) ?$user->invitation_code : "",
            'subscribe' => optional($user->detail)->subscribe,
            'is_per' => $is_per
        ];
        if (!empty($card)){
            //如果修改头像就使用新头像
            if ($card->head_picture){
                $data['portrait'] = $card->head_picture;
            }
        }
        $temp = UserTemplate::where('user_id', $user->id)->where('is_default', 1)->first();
        if (empty($temp)){
            $temp = DB::table('personal_page_template_user')->where('user_id', $user->id)->orderByDesc('created_at')->first();
        }
        $market = DB::table('user_marketing')->where('user_id', $user->id)->first();
        $data['has_usertemp'] = $temp?['id' => $temp->id, 'type' => $temp->type]:false;
        $data['has_market'] = $market?$market->id:false;
        //名片人气显示网页人气
        //$popularity = DB::table('personal_page_template_user')->where('user_id', $user->id)->sum('popularity');
        $data['popularity'] = UserTemplate::popularity($user->id);
        $data['popularity_today'] = UserTemplate::popularityToDay($user->id);
        return $this->renderJson($data);
    }



    public function info(Cards $cards)
    {
        $card = $cards->where('user_id', '=', $this->user->id)->first();
        if ($card == null) {
            return $this->renderJson([
                'head_picture' => isset($this->user->detail->head_portrait) ? $this->user->detail->head_portrait : "",
                'user_name' => '',
                'mobile_phone' => $this->user->phone,
                'telephone' => $this->user->phone,
                'company' => '',
                'industry' => '',
                'profession' => '',
                'wechat_code' => '',
                'is_show_phone' => '',
                'qq' => '',
                'email' => '',
                'website' => '',
                'province' => '',
                'city' => '',
                'area' => '',
                'address_detail' => '',
                'motto' => ''
            ]);
        }
        return $this->renderJson($card);
    }

    // 是否显示手机
    public function isShowPhone(Request $request){
        $is_show_phone = $request->input('is_show_phone');
        if (empty($is_show_phone)) $is_show_phone = 0;
        Cards::where('user_id', '=', $this->user->id)->update(['open_phone'=>$is_show_phone]);
        return $this->success();
    }

    public function feedback(Request $request)
    {
        $this->validate($request, [
            'phone' => [
                'regex:/^(\+?0?86\-?)?((13\d|14[57]|15[^4,\D]|17[3678]|18\d)\d{8}|170[059]\d{7})$/'
            ],
            'content' => 'required|max:200'
        ], [
            'phone.regex' => '手机号错误',
            'content.required' => '内容不能为空'
        ]);

        $data = ['type' => request('type', 1), 'phone' => request('phone'), 'content' => request('content'), 'user_id' => $request->user()->id];
        if ($request->has('images')){
            $data['images'] = json_encode($request->input('images'));
        }
        $r = DB::table('feedback')->insert($data);
        if ($r) {
            return $this->success();
        }
        return $this->error('错误，请稍后重试');
    }

    public function user(Cards $cards, User $u)
    {
        $this->validate(request(), [
            'user_id' => 'required'
        ], [
            'user_id.required' => '用户id不能为空'
        ]);
        $card = $cards->where('user_id', '=', request('user_id'))->first();
        $is_perfect = 1;
        if (empty($card->user_name)){
            $is_perfect = 0;
        }
        $user = $u->find(request('user_id'));
        $data = [
            'id' => $user->id,
            'name' => $user->cards?$user->cards->user_name:'',
            'portrait' => $card?$card->head_picture:$user->detail?$user->detail->head_portrait:'',
            'phone' => $user->phone,
            'industry' => $user->cards?$user->cards->industry:'',
            'profession' => $user->cards?$user->cards->profession:'',
            'company' => $user->cards?$user->cards->company:'',
            'qq' => $user->cards?$user->cards->qq:'',
            'email' => $user->email,
            'vip_level' => 0,
            'card_id' => $card?$card->id:0,
            'is_perfect' => $is_perfect,
            'motto' => $user->cards?$user->cards->motto : '',
        ];
        if (!empty($card)){
            //如果修改头像就使用新头像
            if ($card->head_picture){
                $data['portrait'] = $card->head_picture;
            }
        }

        return $this->renderJson($data);
    }


}
